#!/bin/bash
#加载变量
server_name=xxx
source_code_dir=xxx
type_process=xxx
source_process=xxx
project_name=xxx
update_dir=/app/update/$project_name
back_time=`date +%Y%m%d%H%M`
backup_dir=/app/backup/$project_name
backup_file=$backup_dir/"$back_time"_"$server_name".zip
zip_name="$server_name".zip

upcode(){
    #创建对应目录
    mkdir -p  $backup_dir
    if [[  $server_name = core  ]]  ||  [[  $server_name = report  ]];then
        rm -rf $source_process/work/*  $source_process/temp/*;
        if [[ $project_name = zhjt ]];then
            echo no
        else
            cd $source_code_dir/ &&  rm -rf  app   htm  html  js jsp  lib  login META-INF web  WEB-INF     
        fi
    else
        if [[  $server_name = mobile  ]];then
            cd $source_code_dir && rm -rf $server_name/*
        elif  [[  $server_name = web  ]];then
            cd $source_code_dir && rm -rf $server_name/*
        elif  [[  $server_name = admin  ]];then
            cd $source_code_dir && rm -rf $server_name/*
        else
            echo "no"
        fi
    fi
    #解压缩
    unzip -oq $update_dir/$zip_name -d $source_code_dir/
    #备份,只备份生产环境
    #mv $update_dir/$zip_name $backup_file
    #只保留最新10个备份压缩包
    #ls -t $backup_dir/ | awk 'NR>=10' |xargs rm -f
}

restart_process(){
    #不同类型给与不同重启方式
    #type_process=`echo $source_process_dir | awk  -F '|' '{print $1}'`
    #source_process=`echo $source_process_dir | awk  -F '|' '{print $2}'`
    #重启进程类型(docker,systemctl,脚本.tomcat)
    if [[ $project_name == kkdt ]];then
        echo "请手动重启"
    else
        if [[ $type_process == docker  ]];then
            docker restart $source_process
        elif  [[ $type_process == systemctl  ]];then
            systemctl restart $source_process    
        elif  [[ $type_process == /bin/bash  ]];then
            /bin/bash $source_process
        elif  [[ $type_process == wethink  ]];then
            if [[  $server_name = core  ]]  ||  [[  $server_name = report  ]];then
                pgrep -f $source_process | xargs kill -9
                sleep 1
                $source_process/bin/startup.sh
            else
                echo "前端无需重启进程"
                #$source_process -t
                #if [[  $? == 0 ]];then
                #    $source_process -s reload
                #fi   
            fi
        else
            echo "no $source_process"  
        fi
    fi
}

upcode
restart_process